package com.lagou.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

/**
 * 监控lagou_business数据库下的表
 *
 * @author Fxz
 * @version 1.0
 * @date 2021-10-05 19:16
 */
@Slf4j
@AllArgsConstructor
@CanalEventListener
public class BusinessListener {

    private final RabbitTemplate rabbitTemplate;

    /**
     * 设置监控点 监控对应数据库下的表
     *
     * @param entryType
     * @param rowData
     */
    @ListenPoint(schema = "lagou_business", table = {"tb_ad"})
    public void adUpdate(CanalEntry.EntryType entryType, CanalEntry.RowData rowData) {
        log.info("tb_ad表中的数据发生变化");
        //将修改后的数据的position发送到MQ tb_ad表存储了网站首页所有的缓存信息 不仅仅是首页广告
        rowData.getAfterColumnsList().forEach(column -> {
            if("position".equals(column.getName())){
                log.info("发送消息到MQ ad_update_queue "+column.getValue());
                //发送position的值到MQ
                rabbitTemplate.convertAndSend("","ad_update_queue",column.getValue());
            }
        });

    }
}
